package com.neuedu.his.dao;


import com.neuedu.his.common.utils.DBUtils;
import com.neuedu.his.entity.ConstantsItem;

import java.util.List;
import java.util.Map;

public class ConstantsItemDao {

    /**
     * 查询集合
     */
    public List<ConstantsItem> selectList(String where , Object ... params){
        String sql =" select * from constants_item ";
        if(where != null && !"".equals(where)){
            sql += where;
        }
        List<ConstantsItem> constantsItems = DBUtils.selectList(ConstantsItem.class, sql, params);

        return constantsItems;
    }

    /**
     * 分页查询
     * @param where
     * @param pageNo
     * @param size
     * @param params
     * @return
     */
    public List<ConstantsItem> selectPage(String where , int pageNo,int size ,Object ... params){
        String sql =" select * from constants_item ";
        if(where != null && !"".equals(where)){
            sql += where;
        }

        sql += " limit "+(pageNo-1)*size+"   ,"+size;

        System.out.println("sql = " + sql);
        List<ConstantsItem> constantsItems = DBUtils.selectList(ConstantsItem.class, sql, params);

        return constantsItems;
    }


    /**
     * 查询单条结果
     */
    public ConstantsItem selectOne(String where ,Object ... params){
        String sql =" select * from constants_item ";
        if(where != null && !"".equals(where)){
            sql += where;
        }
        return DBUtils.selectOne(ConstantsItem.class, sql, params);
    }


    /**
     * 查询总条数
     */
    public long selectCount(String where ,Object ... params){
        String sql =" select * from constants_item ";
        if(where != null && !"".equals(where)){
            sql += where;
        }

        sql = "select count(1) count from ("+ sql  +") tab   ";
        Map map = DBUtils.selectMap( sql , params);
        long count = (long) map.get("count");
        return count;
    }




    /**
     * 根据数据进行插入
     * @param constantsItem
     * @return
     */
    public int insert(ConstantsItem constantsItem){
        String sql = "insert into constants_item(       cation   ,       code   ,       type ,type_name  ) values(   ?   ,     ?   ,     ?  ,?    )";
        return DBUtils.update( sql,    constantsItem.getCation()    ,     constantsItem.getCode()    ,     constantsItem.getType(),constantsItem.getTypeName() );
    }


    /**
     * 根据主键更新一条
     * @param constantsItem
     * @return
     */
    public int updateById(ConstantsItem constantsItem){
        String sql = " update constants_item set     cation = ?   ,       code = ?   ,       del = ?   ,       type = ? , type_name = ?   where  item_id = ?  ";

        return DBUtils.update( sql,
                constantsItem.getCation()  ,
                constantsItem.getCode()  ,
                constantsItem.getDel()  ,
                constantsItem.getType()  ,
                constantsItem.getTypeName()  ,
                constantsItem.getItemId() );
    }



    /**
     * 根据主键更新一条
     * @param id
     * @return
     */
    public int deleteById(Integer id){
        String sql = " delete from constants_item  where item_id = ?";
        return DBUtils.update( sql, id);

    }


}